package com.tgu.home_action.controller.admin;

import com.tgu.home_action.pojo.*;
import com.tgu.home_action.pojo.annotation.ApiJsonObject;
import com.tgu.home_action.pojo.annotation.ApiJsonProperty;
import com.tgu.home_action.service.FamilyService;
import com.tgu.home_action.service.admin.AdminFamilyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;

@Api(tags = "管理员的家属管理")
@RestController
@PreAuthorize("hasAnyRole('admin','superAdmin','districtAdmin')")
@RequestMapping("/admin/family")
public class AdminFamilyController {
    @Autowired
    private AdminFamilyService adminFamilyService;
    @Autowired
    private FamilyService familyService;

    @ApiOperation("查询家属用户")
    @GetMapping
    public ResponseData<Page<Family>> getFamily(Family family) {
        System.out.println(family);
        return adminFamilyService.getFamily(family);
    }

    @ApiOperation("通过Id查询")
    @GetMapping("/id")
    public ResponseData<Family> getById(@RequestParam Integer familyId) {
        return familyService.getById(familyId);
    }

    @ApiOperation("修改家属信息")
    @PutMapping
    public ResponseData<Object> updateFamilyInfo(@Validated(Family.update.class) @RequestBody Family family) {
        return adminFamilyService.updateFamilyInfo(family);
    }

    @ApiOperation("修改头像")
    @PutMapping("/avatar")
    public ResponseData<Object> updateFamilyAvatar(@ApiJsonObject(name = "AdminUpdateFamilyAvatar", value = {
            @ApiJsonProperty(key = "id", description = "志愿者信息ID"),
            @ApiJsonProperty(key = "avatarUrl", description = "头像Url")
    }) @RequestBody Map<String, Object> param) {
        Integer id = (Integer) param.get("id");
        String avatarUrl = (String) param.get("avatarUrl");
        return adminFamilyService.updateFamilyAvatar(id, avatarUrl);
    }

    @ApiOperation("查看老人信息")
    @GetMapping("/oldMan")
    public ResponseData<List<OldMan>> getOldMen(@RequestParam Integer familyId) {
        return adminFamilyService.getOldMen(familyId);
    }

    @ApiOperation("修改老人信息")
    @PutMapping("/oldMan")
    public ResponseData<Object> updateOldMan(@Validated(OldMan.update.class) @RequestBody OldMan oldMan) {
        return adminFamilyService.updateOldMan(oldMan);
    }

    @ApiOperation("查询案件信息(familyId必填，state,pageNo,pageSize可选（后两个必须同时选择）)")
    @GetMapping("/case")
    public ResponseData<Page<Case>> getCases(@Validated(Case.familyCase.class) Case nowCase) {
        return adminFamilyService.getCases(nowCase);
    }

    @ApiOperation("查询老人对应的案件")
    @GetMapping("/oldMan/case")
    public ResponseData<Page<Case>> getOldManCases(@Validated(Case.oldManCase.class) Case nowCase) {
        return adminFamilyService.getOldManCases(nowCase);
    }

    //
    @ApiOperation("删除家属用户")
    @DeleteMapping
    public ResponseData<Object> delFamily(@RequestParam Integer familyId) {
        return adminFamilyService.delFamily(familyId);
    }

    @ApiOperation("删除老人")
    @DeleteMapping("/oldMan")
    public ResponseData<Object> deleteOldMan(@RequestParam Integer oldManId) {
        return adminFamilyService.delOldMan(oldManId);
    }

}
